package com.xpgk.mvc.increaseWeight;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.plugin.activerecord.Record;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.statistic.daySales.DaySalesStatisticService;
import com.xpgk.tools.ToolDateTime;
public class IncreaseWeightController extends BaseController{
		
	private DaySalesStatisticService daySalesStatisticService;
	//涨称
	public void index(){
		Date date = ToolDateTime.getDate();
		Organization organ=Organization.dao.findById(getCUserOrganIds());
		setAttr("organization",organ);
		setAttr("date",date);
		StringBuffer url = getRequest().getRequestURL();
		if (url.indexOf("flatWeight")>0) {
			setAttr("pc",0);
			render("increaseWeight.html");
		}else if (url.indexOf("increaseWeight")>0){
			Code parentCode = Code.cacheGet(Code.getCode("code_yjsz"));
			List<Code> codeList = Code.cacheGetChild(parentCode.getPKValue());
			float count = 3;
			for (Code code : codeList) {
				String id =code.get(Code.column_ids);
				if(id.equals("70a9d8f0f37943d2a2ba3ee05de22254")){
					count = Float.parseFloat((String) code.get(Code.column_code));
				}
			}
			setAttr("zc",count);
			render("increaseWeight.html");
		}else if (url.indexOf("loseWeight")>0){
			Code parentCode = Code.cacheGet(Code.getCode("code_yjsz"));
			List<Code> codeList = Code.cacheGetChild(parentCode.getPKValue());
			float count = 3;
			for (Code code : codeList) {
				String id =code.get(Code.column_ids);
				if(id.equals("eb5b3258ce264e2f911b2d2aa2b90a23")){
					count = Float.parseFloat((String) code.get(Code.column_code));
				}
			}
			setAttr("dc",-count);
			render("loseWeight.html");
		}
		
	}
	/*
	 * 查询结果导出
	 */
	public void excel() throws UnsupportedEncodingException{
		String organIds = getPara("organIds");
		String organName = getPara("orgName");
		organName = new String(organName.getBytes("ISO-8859-1"), "UTF-8"); 
		String date = getPara("date");
		String zc = getPara("zc");
		String pc = getPara("pc");
		String dc = getPara("dc");

		String organizationName = organIds.equals("")?"":("门店："+organName+"，");
		String dateSearch = date.equals("")?"":("日期："+date+"，");
		String search = organIds.equals("")?"":(organizationName+dateSearch);
		String excelname = "平称顾客";
		
		splitPage.setExport(true);
		Map<String, Object> map =splitPage.getQueryParam();
		map.put("organIds", organIds);
		map.put("date", date);
		if (StringUtils.isNotBlank(zc)) {
			excelname = "涨称顾客";
			map.put("zc", zc);
		}else if (StringUtils.isNotBlank(pc)) {
			map.put("pc", pc);
		}else if (StringUtils.isNotBlank(dc)) {
			excelname = "掉称顾客";
			map.put("dc", dc);
		}
		paging(splitPage,IncreaseWeightSQL.increaseWeight_splitPageSelect,IncreaseWeightSQL.increaseWeight_splitPageFrom);
		List<Record> result =(List<Record>) splitPage.getList();
		render(new IncreaseWeightXLS(result,excelname ,search));
	}
	
	/**
	 * 涨秤顾客查询
	 */
	public void queryIncrease(){
		List<Record> records = new ArrayList<>();
		
		String sql = "SELECT * FROM sb_customer_detail WHERE organIds = ? and dataStatus in ('1','2')";
		String csSql = "SELECT * from sb_customer_service  where DATE_FORMAT(startTime, '%Y-%m-%d') <  ?    AND    customerID = ?   and dataStatus in ('1','2')   ORDER BY  startTime    DESC   LIMIT 0,1";
		List<CustomerDetail> cdetList = CustomerDetail.dao.find(sql,getCUserOrganIds());
		if(cdetList!=null && cdetList.size()>0){
			for(CustomerDetail cdet:cdetList){
				Record  r = new Record();
				String cusIds = cdet.getStr(CustomerDetail.column_customerIds);
				CustomerService cs = CustomerService.dao.findFirst(csSql,ToolDateTime.format(new Date(), ToolDateTime.pattern_ymd),cusIds);
				if(cs!=null){
					if(cs.getFloat(CustomerService.column_weight)!=null && cs.getFloat(CustomerService.column_lastWeight)!=null){
						if(cs.getFloat(CustomerService.column_weight)>=cs.getFloat(CustomerService.column_lastWeight)){
							r.set("ids", cusIds);
							r.set("NAME", cdet.getStr(CustomerDetail.column_name));
							r.set("phonenumber", cdet.getStr(CustomerDetail.column_phone));
							r.set("stantardweight", cdet.getFloat(CustomerDetail.column_targetWeight));
							r.set("tolosepounds", cdet.getFloat(CustomerDetail.column_delWeight));
							r.set("initialweight", cdet.getFloat(CustomerDetail.column_firstWeight));
							r.set("lastweight", cs.getFloat(CustomerService.column_lastWeight));
							r.set("firststoredate", ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_ymd));
							r.set("bcweight", cs.getFloat(CustomerService.column_weight));
							r.set("zcjs", cs.getFloat(CustomerService.column_weight)-cs.getFloat(CustomerService.column_lastWeight));
							records.add(r);
						}
					}
				}
				
			}
		}
		
		renderJson(records);
		/*String json=paging(splitPage,IncreaseWeightSQL.increaseWeight_splitPageSelect,IncreaseWeightSQL.increaseWeight_splitPageFrom);
		renderJson(json);*/
	}
	/**
	 * 涨秤顾客查询
	 */
	public void querylose(){
List<Record> records = new ArrayList<>();
		
		String sql = "SELECT * FROM sb_customer_detail WHERE organIds = ? and dataStatus in ('1','2')";
		String csSql = "SELECT * from sb_customer_service  where DATE_FORMAT(startTime, '%Y-%m-%d') <  ?    AND    customerID = ?   and dataStatus in ('1','2')   ORDER BY  startTime    DESC   LIMIT 0,1";
		List<CustomerDetail> cdetList = CustomerDetail.dao.find(sql,getCUserOrganIds());
		if(cdetList!=null && cdetList.size()>0){
			for(CustomerDetail cdet:cdetList){
				Record  r = new Record();
				String cusIds = cdet.getStr(CustomerDetail.column_customerIds);
				CustomerService cs = CustomerService.dao.findFirst(csSql,ToolDateTime.format(new Date(), ToolDateTime.pattern_ymd),cusIds);
				if(cs!=null){
					if(cs.getFloat(CustomerService.column_weight)!=null && cs.getFloat(CustomerService.column_lastWeight)!=null){
						if(cs.getFloat(CustomerService.column_weight)<cs.getFloat(CustomerService.column_lastWeight)){
							r.set("ids", cusIds);
							r.set("NAME", cdet.getStr(CustomerDetail.column_name));
							r.set("phonenumber", cdet.getStr(CustomerDetail.column_phone));
							r.set("stantardweight", cdet.getFloat(CustomerDetail.column_targetWeight));
							r.set("tolosepounds", cdet.getFloat(CustomerDetail.column_delWeight));
							r.set("initialweight", cdet.getFloat(CustomerDetail.column_firstWeight));
							r.set("lastweight", cs.getFloat(CustomerService.column_lastWeight));
							r.set("firststoredate", ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_ymd));
							r.set("bcweight", cs.getFloat(CustomerService.column_weight));
							r.set("zcjs", cs.getFloat(CustomerService.column_weight)-cs.getFloat(CustomerService.column_lastWeight));
							records.add(r);
						}
					}
				}
				
			}
		}
		
		renderJson(records);
		/*String json=paging(splitPage,IncreaseWeightSQL.increaseWeight_splitPageSelect2,IncreaseWeightSQL.increaseWeight_splitPageFrom2);
		renderJson(json);*/
	}
	/**
	 * 回访
	 */
	public void visit(){
		render("visit.html");
	}
	/**
	 * 查询
	 */
	public void query(){
		render("query.html");
	}
}
